<?
require_once("class_GenericRowInt.php");
require_once("class_IndexToastName.php");

/********************************************************************************************************/
/* This class represents one row from any table which hase non composit not autoincremented PRIMARY KEY */
/********************************************************************************************************/

class TableToastName extends GenericRowInt {
    private $index;

    public function TableToastName($in_id=false) {
	parent::__construct('table_toast_name','tn_id',$in_id);
	if ($in_id) {
	    $this->index=new IndexToastName($in_id);
	} else {
	    $this->index=new IndexToastName;
	}
    }

    public function create_index($t_i_relid,$t_i_relname) {
	if (!$this->index->is_existing()) {
	    $this->index->set_field('tn_id',$this->id);
	    $this->index->set_field('obj_oid',$t_i_relid);
	    $this->index->set_field('idx_name',$t_i_relname);
	    $this->index->save();
	}
    }
    public function get_stat($time_id,$prod_sql) {
	$prod_sql->select_c("SELECT
pg_stat_get_numscans(oid) AS seq_scan,
pg_stat_get_tuples_returned(oid) AS seq_tup_read,
pg_stat_get_tuples_fetched(oid) AS seq_tup_fetch,
pg_stat_get_tuples_inserted(oid) AS n_tup_ins,
pg_stat_get_tuples_updated(oid) AS n_tup_upd,
pg_stat_get_tuples_deleted(oid) AS n_tup_del,
pg_stat_get_tuples_hot_updated(oid) AS n_tup_hot_upd,
pg_stat_get_live_tuples(oid) AS n_live_tup,
pg_stat_get_dead_tuples(oid) AS n_dead_tup,
pg_stat_get_blocks_fetched(oid) AS heap_blks_fetch,
pg_stat_get_blocks_hit(oid) AS heap_blks_hit
FROM pg_class
WHERE oid=".$this->get_field('obj_oid'));
	$stat=$prod_sql->get_row_hash();

	$this->sql->non_select_c("INSERT INTO table_toast_stat VALUES ("
.$this->id.","
.$time_id.","
.$stat['seq_scan'].","
.$stat['seq_tup_read'].","
.$stat['seq_tup_fetch'].","
.$stat['n_tup_ins'].","
.$stat['n_tup_upd'].","
.$stat['n_tup_del'].","
.$stat['n_tup_hot_upd'].","
.$stat['n_live_tup'].","
.$stat['n_dead_tup'].","
.$stat['heap_blks_fetch'].","
.$stat['heap_blks_hit'].")");

	$this->index->get_stat($time_id,$prod_sql);
    }


    public function __toString() {
	return $this->get_field('tbl_name');
    }
}

?>
